import base64
import os
import shutil

s = os.sep  # 根据unix或win，s为\或/

root = 'image/'
cutoutStr = 'base64'


for rt, dirs, files in os.walk(root):
    for name in files:
        # print(os.path.splitext(name)[0])

        filepath = rt + name

        new_filepath = rt + 'new_' + os.path.splitext(name)[0] + '.jpg'

        shutil.copyfile(filepath, new_filepath)

        with open(new_filepath, 'rb+') as file:
            base64Str = base64.b64encode(file.read()).decode()  # 读取文件内容，转换为base64编码
            print(len(base64Str))
            # if (base64Str.find('data') == 0):
            #     base64Str = base64Str.split(cutoutStr)[1]
            #     print(len(base64Str))

            imgdata = base64.b64decode(base64Str)

            file.write(imgdata)

            file.close()
        
    # f = open(files, 'rb')  # 二进制方式打开图文件
    
    # with open(files, 'wb') as file:

    # print(files)

    # filepath = rt + files

    # new_filepath = rt + os.path.splitext(files)[0] + '_new.jpg'

    

    # shutil.copyfile(filepath, new_filepath)


    # with open(filepath, 'rb') as file:

    #     os.path.splitext()

    #     base64Str = base64.b64encode(file.read())  # 读取文件内容，转换为base64编码
        
    #     if (base64Str.index('data:') == 0):
    #         base64Str = base64Str.split('base64,')[1]

    #     imgdata = base64.b64decode(base64Str)

    #     file.write(imgdata)